"use strict";
/* Controllers */
app.controller("userListController", ['$scope', '$location', '$rootScope', '$routeParams', 'userListService', function ($scope, $location, $rootScope, $routeParams, userListService) {

    var vm = $scope.vm = {};
    vm.p = {};
    vm.result = {};

    var errorFn = function (result) {
        alert(result);
    };

    $scope.listUser = function () {
        var vm = $scope.vm;
        vm.result.ms = "...";
        var params = {};
        var procFn = function (response) {
            vm.result.ms = response.ms;
            var data = response.data;
            if (data == null) return;
            vm.result.data = data;
            $('#list_table').bootstrapTable('load', data);
        };
        userListService.listUser(params, procFn, errorFn);
    };

    $scope.delItem = function(id) {
        var msg = "您真的确定要删除"+id+"吗？";
        if (confirm(msg)==false){
            return false;
        }
        var params = {id: id};
        var procFn = function (response) {
            var data = response.data;
            if (data == null) return;
            alert("删除成功："+response.message);
            $scope.listUser();
        };
        userListService.delUser(params, procFn, errorFn);
    };

    $scope.initializeController = function () {
        vm.result.ms = '';
        var $resultTable = $('#list_table');
        $resultTable.bootstrapTable({
             pageList: [30, 50, 100, 300, 500, 1000], pageSize: 30
        });
        $scope.listUser();
    };

    $scope.initializeController();

}]);


/* Services */
app.service('userListService', ['ajaxService', function (ajaxService) {

    this.listUser = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/user/user_list.do", successFunction, errorFunction);
    };
    this.delUser = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/user/delUser.do", successFunction, errorFunction);
    };
}]);

function userIDFmt(value, row, index) {
    return [
        '<a href="#/star/user/user_add/', value, '" target="_blank"> ', "编辑", '</a>',
        '<a href="javascript:void(0)" onclick="delItem(', value, ')"> ', " 删除", '</a>',
        '<a href="#/star/user/user_group" target="_blank"> ', " 加入组", '</a>'
    ].join('');
}
function delItem(id) {
    //var $scope = angular.element($("#list_table")).scope();
    var $scope = angular.element('.row').scope();
    $scope.delItem(id);
}

